﻿
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Reflection;
using KWatkins.FluentMoqBuilder;
using System.Web.Mvc;
using RazorEngine;
using RazorEngine.Templating;
using TSharp.Core.TestUtil;
using TSharp.Core;
using System.Collections;
using System.Text;
using TSharp.Core.Repository;
namespace RazorTests
{


    /// <summary>
    ///这是 RazorTest 的测试类，旨在
    ///包含所有 RazorTest 单元测试
    ///</summary>
    [TestClass()]
    public class RazorTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        // 
        //编写测试时，还可使用以下特性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            new Microsoft.CSharp.RuntimeBinder.RuntimeBinderException().ToString();
            new System.Web.Mvc.EmptyResult().ToString();
            TestHelper.SetupTest(testContext.GetType());


        }
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///Services 的测试
        ///</summary>
        [TestMethod()]
        public void ServicesTest()
        {
        }
        public class Person
        {
            public string Name { get; set; }

            public List<string> Friends { get; set; }

        }

        /// <summary>
        ///Parse 的测试
        ///</summary>
        [TestMethod()]
        public void ParseTest()
        {

            string template = "Hello @Model.Name! Welcome to Razor!";
            // template = "Hello   Welcome to Razor!";
            //string expected = "Hello World! Welcome to Razor!";

            string actual = null;
            try
            {

                //  actual = Razor.Parse(template, new Person() { Name = "World" });
                actual = Razor.Parse(template, new { Name = "World" });
            }
            catch (TemplateCompilationException ex)
            {
                ex.Errors.ToLookup(x =>
                {

                    Console.WriteLine(string.Format("{2}#{0},Txt:{1}",
                        x.ErrorNumber, x.ErrorText, x.Line));
                    return true;
                });
            }


            Console.WriteLine(actual);
            //  Assert.AreEqual(expected, actual);
        }

        class Test : TemplateBase<System.Dynamic.DynamicObject>
        {

        }
        [TestMethod()]
        public void ParseTest1()
        {
            string template =
            @"<html>
      <head>
        <title>Hello @Model.Name</title>
      </head>
      <body>
        Email: @Html.TextBoxFor(m => m.Email)
      </body>
    </html>";

            string key = "testkey";

            try
            {
                Razor.Compile(template, typeof(PageModel), key);
            }
            catch (TemplateCompilationException ex)
            {
                foreach (var e in ex.Errors)
                    Console.Out.WriteLine(e.ErrorText);
                throw ex;
            }


            var model = new PageModel { Name = "World", Email = "someone@somewhere.com" };
            var result = Razor.Run(key, model);
            Console.WriteLine(result);
            result = Razor.Run(key, new PageModel { Name = "World1", Email = "1@somewhere.com" });
            Console.WriteLine(result);
        }

        [TestMethod]
        public void ParseTest2()
        {
            Console.WriteLine(string.Format("TSharp_{0:N}", Guid.NewGuid()));

            string name = "Test User Name";

            MailMessage simpleHelloWorldMail =
                new MailMessage("alex@alexonasp.net", "alex@alexonasp.net");
            string mailTemplate = "Hello @Model.Name! Welcome to Razor!";

            string body = Razor.Parse(mailTemplate, new { Name = name });
            simpleHelloWorldMail.Body = body;

            sendMail(simpleHelloWorldMail);

            Console.WriteLine("Plain-Text-Mail versandt...");

            MailMessage htmlHelloWorldMail =
                new MailMessage("alex@alexonasp.net", "alex@alexonasp.net");

            htmlHelloWorldMail.IsBodyHtml = true;


            string htmlTemplate =
                @"<html>
					<head>
						<style type=\""text/css\"">
							body { font-family: Arial; }
						</style>
					</head>
					<body>
						<h1>Hello @Model.Name</h1>
						<p>Welcome to Razor Templating...</p>
					</body>
				</html>";

            var model = new { Name = name };

            try
            {
                string htmlBody = Razor.Parse(htmlTemplate, model);
                htmlHelloWorldMail.Body = htmlBody;
                sendMail(htmlHelloWorldMail);
            }
            catch (TemplateCompilationException ex)
            {
                ex.Errors.ToLookup(
                   x =>
                   {
                       Console.WriteLine(string.Format("{2}#{0},Txt:{1}",
                        x.ErrorNumber, x.ErrorText, x.Line));
                       return x.FileName;
                   });
            }




            Console.WriteLine("HTML-Mail versandt...");
            Console.ReadLine();
        }
        [TestMethod]
        public void ThreadRazorParseTest()
        {
            // Console.Write(string.Format("TSharp_{0:N}", Guid.NewGuid()));
            string name = "World";

            MailMessage simpleHelloWorldMail =
                new MailMessage("alex@alexonasp.net", "alex@alexonasp.net");
            string mailTemplate = "Hello @Model.Name! Welcome to Razor!";

            string body = Razor.Parse(mailTemplate, new { Name = name });
            simpleHelloWorldMail.Body = body;

            sendMail(simpleHelloWorldMail);

            Console.WriteLine("Plain-Text-Mail versandt...");

            MailMessage htmlHelloWorldMail =
                new MailMessage("alex@alexonasp.net", "alex@alexonasp.net");

            htmlHelloWorldMail.IsBodyHtml = true;


            string htmlTemplate =
                @"<html>
					<head>
						<style type=\""text/css\"">
							body { font-family: Arial; }
						</style>
					</head>
					<body>
						<h1>Hello @Model.Name</h1>
<h2>@Url.Action(""a"");</h2>
						<p>Welcome to Razor Templating...</p>
					</body>
				</html>";

            var model = new { Name = name };

            try
            {
                string htmlBody = Razor.Parse(htmlTemplate, model);
                htmlHelloWorldMail.Body = htmlBody;
                sendMail(htmlHelloWorldMail);
                Console.WriteLine("HTML-Mail versandt...");
            }
            catch (TemplateCompilationException)
            {
                //ex.WriteErrors(Console.Out);
            }
            Console.ReadLine();
        }

        private static void sendMail(MailMessage mail)
        {
            Console.WriteLine(mail.Body);
        }
        [TestMethod]
        public void TestCompileWithAnonymous()
        {
            string htmlTemplate =
                @"<html>
					<head>
						<style type=\""text/css\"">
							body { font-family: Arial; }
						</style>
					</head>
					<body>
						<h1>Hello @Model.Name</h1>
						<p>Welcome to Razor Templating...</p>
					</body>
				</html>";
            try
            {
                htmlTemplate = @"<h1>Hello @Model.Name</h1>";
                Razor.Compile(htmlTemplate, new { }.GetType(), "test");
            }
            catch (TemplateCompilationException)
            {
                //Console.WriteLine(ex.FormatMessage);
            }


        }
        [TestMethod]
        public void DynamicDbCtx()
        {
        }

    }
}
